#version 330

uniform mat3x3 u_viewInvTrans;

uniform int u_numLights;
uniform vec3 u_lightColor[8];
uniform vec3 u_lightDirection[8];

#ifdef TX_COLOR
uniform sampler2D u_Texture;
#endif

in vec3 fs_Normal;
in vec3 fs_Position;

#ifdef TX_COLOR
in vec2 fs_txCoord;
#else
in vec3 fs_Color;
#endif

out vec3 out_Color;

vec3 lightCalc(in vec3 dir, in vec3 normal, in vec3 color) {
	return color*(max(dot(-dir, normal), 0));
}

void main(void)
{
/*
	if(!gl_FrontFacing) {
		discard;
		return;
	}
*/
	float blendWeight = .4f;
	vec3 lightSum = vec3(.0f,.0f,.0f);
	vec3 normal = normalize(fs_Normal);
	
	for(int i = 0; i < u_numLights; ++i)
		lightSum += blendWeight*lightCalc(u_viewInvTrans*u_lightDirection[i], normal, u_lightColor[i]);
	
#ifdef TX_COLOR
	out_Color = (texture(u_Texture, fs_txCoord).rgb * .1f)*lightSum;
#else
	out_Color = (fs_Color*.1f)*lightSum;
	//out_Color = (fs_Color*.8f)*max(0.0f, dot(fs_Normal, -normalize(fs_Position)));
	//out_Color = vec3(0.0f, .5f, 0.0f);
#endif
	
    //out_Color = vec3(max(0.0f,dot(-normalize(fs_Position.xyz),fs_Normal))*.5f + .5f);
    //out_Color = vec3(1.0f, 0.0f, 0.0f);
}